﻿using System;
using UnityEngine;
using System.Collections;
using DG.Tweening;
using UnityEngine.UI;
using UnityEngine.EventSystems;

namespace MobileMedia
{
    public class DragMoveScrollView : ScrollRect
    {
        private float m_maxScale = 5;
        private float m_minScale = 1f;
        private Touch oldTouch1;
        private Touch oldTouch2;
        public bool isScenePrefabControl = true;
        private int touchNum = 0;

        private Camera uiCamera;
        private bool m_enableOperate = true;

        private int rotateAngel = 0;
        private float rotateTime = 0.6f;

        public void SetContentDefaultSize(float width, float height)
        {
            base.content.sizeDelta = new Vector2(width, height);
        }

        public void SetScaleLimit(float min, float max)
        {
            m_minScale = min;
            m_maxScale = max;
        }

        public void Rotate2Left(Action callBack = null)
        {
            if (!m_enableOperate) return;
            StopMovement();
            m_enableOperate = false;
            rotateAngel += 90;
            content.DORotate(new Vector3(0, 0, rotateAngel), rotateTime).onComplete = () =>
            {
                m_enableOperate = true;
                callBack?.Invoke();
            };
        }

        public void Rotate2Right(Action callBack = null)
        {
            if (!m_enableOperate) return;
            StopMovement();
            m_enableOperate = false;
            rotateAngel -= 90;
            content.DORotate(new Vector3(0, 0, rotateAngel), rotateTime).onComplete = () =>
            {
                m_enableOperate = true;
                callBack?.Invoke();
            };
        }

        public void ResetRot(Action callBack = null)
        {
            if (!m_enableOperate) return;
            StopMovement();
            m_enableOperate = false;
            rotateAngel = 0;
            content.DORotate(new Vector3(0, 0, rotateAngel), rotateTime).onComplete = () =>
            {
                m_enableOperate = true;
                callBack?.Invoke();
            };
        }

        public void ResetPos(Action callBack = null)
        {
            if (!m_enableOperate) return;
            StopMovement();
            m_enableOperate = false;
            content.DOAnchorPos(Vector2.zero, 0.6f).onComplete = () =>
            {
                m_enableOperate = true;
                callBack?.Invoke();
            };
        }

        public override void OnBeginDrag(PointerEventData eventData)
        {
            if (!m_enableOperate) return;
            if (Input.touchCount > 1)
            {
                return;
            }

            base.OnBeginDrag(eventData);
        }

        public override void OnDrag(PointerEventData eventData)
        {
            if (!m_enableOperate) return;

            if (Input.touchCount > 1)
            {
                touchNum = Input.touchCount;
                return;
            }
            else if (Input.touchCount == 1 && touchNum > 1)
            {
                touchNum = Input.touchCount;
                base.OnBeginDrag(eventData);
                return;
            }

            base.OnDrag(eventData);
        }

        void Update()
        {
            if (!m_enableOperate) return;

            if (uiCamera == null)
            {
                GameObject o = GameObject.Find("UIRoot/UICamera");
                if (o)
                {
                    uiCamera = o.transform.GetComponent<Camera>();
                }
            }

            if (base.content == null) return;

#if UNITY_EDITOR

            float mouseValue = Input.GetAxis("Mouse ScrollWheel");
            if (Mathf.Abs(mouseValue) > 0.002f)
            {
                float scaleValue = mouseValue * 0.5f;
                var scale = base.content.transform.localScale + new Vector3(scaleValue, scaleValue, scaleValue);

                // TODO 偏移到中心
                Vector2 touchCenter = Input.mousePosition;

                if (scale.x >= m_minScale && scale.x <= m_maxScale)
                {
                    base.content.transform.localScale = scale;
                }
                else
                {
                    if (scale.x < m_minScale)
                    {
                        base.content.transform.localScale = new Vector3(m_minScale, m_minScale, m_minScale);
                    }
                    else if (scale.x > m_maxScale)
                    {
                        base.content.transform.localScale = new Vector3(m_maxScale, m_maxScale, m_maxScale);
                    }
                }
            }

#else

        if (isScenePrefabControl)
        {
            if (Input.touchCount >= 2)
            {
                //多点触摸, 放大缩小
                Touch newTouch1 = Input.GetTouch(0);
                Touch newTouch2 = Input.GetTouch(1);
                //第2点刚开始接触屏幕, 只记录，不做处理
                if (newTouch2.phase == TouchPhase.Began)
                {
                    oldTouch2 = newTouch2;
                    oldTouch1 = newTouch1;
                    return;
                }
                //计算老的两点距离和新的两点间距离，变大要放大模型，变小要缩放模型
                float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
                float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
                //两个距离之差，为正表示放大手势， 为负表示缩小手势
                float offset = newDistance - oldDistance;
                //放大因子， 一个像素按 0.01倍来算(100可调整)
                float scaleFactor = offset / 100f;
                Vector3 localScale = base.content.transform.localScale;
                Vector3 scale = new Vector3(localScale.x + scaleFactor,
                    localScale.y + scaleFactor,
                    localScale.z + scaleFactor);
                //在什么情况下进行缩放
                if (scale.x >= m_minScale && scale.x <= m_maxScale)
                {
                    // TODO 偏移到中心

                    base.content.transform.localScale = scale;
                }
                else
                {
                    if (scale.x < m_minScale)
                    {
                        base.content.transform.localScale = new Vector3(m_minScale, m_minScale, m_minScale);
                    }
                    else if(scale.x > m_maxScale)
                    {
                        base.content.transform.localScale = new Vector3(m_maxScale, m_maxScale, m_maxScale);
                    }
                }
                //记住最新的触摸点，下次使用
                oldTouch1 = newTouch1;
                oldTouch2 = newTouch2;
            }
            
        }
#endif


        }
    }
}